package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.group.network.express_plus_app.adapter.MyOrderHistoryAdapter;
import com.example.group.network.express_plus_app.adapter.MyReceiveOrderHistoryAdapter;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.viewmodel.MyOrderHistoryViewModel;
import com.example.group.network.express_plus_app.viewmodel.MyReceiveOrderHistoryViewModel;
import com.example.group.network.express_plus_app.widget.SpacesItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyReceiveOrderHistoryActivity extends AppCompatActivity {

    @BindView(R.id.rv_my_receive_order_history)
    RecyclerView rv_my_receive_order_history;
    private MyReceiveOrderHistoryViewModel myReceiveOrderHistoryViewModel;
    @BindView(R.id.all_income)
    TextView all_income;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_receive_order_history);
        ButterKnife.bind(this);
        setActionBar("我的接单历史");
        myReceiveOrderHistoryViewModel=new ViewModelProvider(this).get(MyReceiveOrderHistoryViewModel.class);
        myReceiveOrderHistoryViewModel.findMyReceiveOrderHistory();
        myReceiveOrderHistoryViewModel.getMyReceiveOrderHistory().observe(this, new Observer<ArrayList<Order>>() {
            @Override
            public void onChanged(ArrayList<Order> orders) {
                float num=0f;
                for (int i=0;i<orders.size(); i++){
                    num=num+orders.get(i).getEstimatedPrice();
                }
                all_income.setText(String.valueOf(num));
                //订单展示
                LinearLayoutManager manager=new LinearLayoutManager(getApplicationContext());
                manager.setOrientation(RecyclerView.VERTICAL);
                rv_my_receive_order_history.setLayoutManager(manager);
                MyReceiveOrderHistoryAdapter adapter=
                        new MyReceiveOrderHistoryAdapter(getApplicationContext(), orders);
                adapter.setOnItemClickListener(adapter);
                rv_my_receive_order_history.setAdapter(adapter);

            }
        });
        rv_my_receive_order_history.setItemAnimator(new DefaultItemAnimator()); //设置列表项增删动画
        rv_my_receive_order_history.addItemDecoration(new SpacesItemDecoration(10));
    }
    public void setActionBar(String title){
        //标题栏设置
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        supportActionBar.setTitle(title);

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}